package com.linhngua.code.gwt.spring;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import com.linhngua.code.hibernate.config.Database;

/**
 * 
 * @author tranxl
 * Use Oracle as an example
 */
@Configuration
@Profile({"dev"})
public class HibernateOracleInitSpring extends HibernateInitSpring {

	@Autowired
	Database database;

	
	

	

	@Override
	@Bean
	DataSource dataSource() {
		org.apache.commons.dbcp.BasicDataSource dataSource = new org.apache.commons.dbcp.BasicDataSource();
		dataSource
				.setDriverClassName("oracle.jdbc.xa.client.OracleXADataSource");
		dataSource.setUrl(database.getJdbcUrl());
		dataSource.setUsername(database.getUserName());
		dataSource.setPassword(database.getPassword());

		configureDataSource(dataSource, database.getValidationQuery());
		applySchema(dataSource);
		return dataSource;
	}

	@Override
	String getHibernateDialect() {
		return "org.hibernate.dialect.Oracle10gDialect";
	}

	@Override
	org.hibernate.cfg.Configuration createConfig() {

		org.hibernate.cfg.Configuration config = new org.hibernate.cfg.Configuration();

		config.setProperty(org.hibernate.cfg.Environment.DRIVER,
				"oracle.jdbc.xa.client.OracleXADataSource");
		config.setProperty(org.hibernate.cfg.Environment.URL,
				database.getJdbcUrl());
		config.setProperty(org.hibernate.cfg.Environment.USER,
				database.getUserName());
		config.setProperty(org.hibernate.cfg.Environment.PASS,
				database.getPassword());
		config.setProperty(org.hibernate.cfg.Environment.DIALECT,
				getHibernateDialect());
		return config;
	}

}
